package com.visikard.ui.adapter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.application.UIMessage;
import com.visikard.item.ItemCouponsBuilder;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.UILImageUtils;
import com.visikard.utility.Utils;

public class CouponsListAdapter extends BaseAdapter implements Filterable,
		SectionIndexer {

	Context context;
	ArrayList<ItemCouponsBuilder> data;
	ArrayList<ItemCouponsBuilder> dataAll;
	LayoutInflater inflater;

	DateFormat formatter;
	DateFormat dateOnlyFormatter;
	DisplayMetrics metrics;
	DateFormat timeFormat;
	Handler mHandler;

	public CouponsListAdapter(Context ct, ArrayList<ItemCouponsBuilder> dt,
			Handler mHandler) {
		this.context = ct;
		this.data = dt;
		this.dataAll = dt;
		this.mHandler = mHandler;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		formatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR);
		dateOnlyFormatter = new SimpleDateFormat(
				Constant.TIME_FORMAT_STR_MONTH_DATE);
		timeFormat = new SimpleDateFormat(Constant.TIME_SHORT_FORMAT_STR,
				Locale.US);
		metrics = context.getResources().getDisplayMetrics();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < data.size()) {
			return data.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder viewHolder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.coupons_list_item, null);
			Utils.overrideFonts(convertView);
			viewHolder = new ViewHolder();

			viewHolder.lyItemContent = (RelativeLayout) convertView.findViewById(R.id.item_ly_content);
			viewHolder.lyItemCoupon = (RelativeLayout) convertView.findViewById(R.id.ly_item_coupons);
			viewHolder.imgCoupon = (ImageView) convertView.findViewById(R.id.image_coupons_kard);
			viewHolder.mTextDetails = (TextView) convertView.findViewById(R.id.text_dertails_coupon);

			viewHolder.btnAudience = (ImageView) convertView.findViewById(R.id.image_coupons_audience);
			viewHolder.btnGive = (ImageView) convertView.findViewById(R.id.image_coupons_give);
			viewHolder.btnStar = (ImageView) convertView.findViewById(R.id.image_coupons_star);

			viewHolder.btnEdit = (Button) convertView.findViewById(R.id.btn_edit);
			viewHolder.btnDelete = (Button) convertView.findViewById(R.id.btn_delete);
			
			viewHolder.drawer = (SlidingDrawer) convertView.findViewById(R.id.drawer);
			viewHolder.btnIcon = (ImageView) convertView.findViewById(R.id.handle);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		final ItemCouponsBuilder item = (ItemCouponsBuilder) getItem(position);
		if (item != null) {
			viewHolder.drawer.close();
			if (item.getCouponName() != null
					&& item.getCouponName().length() > 0)
				viewHolder.mTextDetails.setText(item.getCouponName());
			else
				viewHolder.mTextDetails.setText("");

			int mWidth = metrics.widthPixels;
			int mHeightImg = (2 * mWidth) / 9;
			int paddingVertical = context.getResources().getDimensionPixelSize(
					R.dimen.ly_padding10_5);

			RelativeLayout.LayoutParams lyKard = (RelativeLayout.LayoutParams) viewHolder.lyItemContent
					.getLayoutParams();

			lyKard.width = mWidth;
			lyKard.height = mHeightImg + 2 * paddingVertical;

			RelativeLayout.LayoutParams lyGiftKard = (RelativeLayout.LayoutParams) viewHolder.imgCoupon
					.getLayoutParams();
			lyGiftKard.width = mWidth / 3;
			lyGiftKard.height = mHeightImg;

//			ImageLoader.getInstance(context).loadImageCorner(
//					viewHolder.imgCoupon, item.getImageSpecial(),
//					Device.getDeviceWidth() / 3, BitmapUtils.ROUND_NONE, 0, 0,
//					R.drawable.kard_shadow_default_horizontal, 0, 0);
			UILImageUtils.loadImage(viewHolder.imgCoupon, //image view
					item.getImageSpecial(), //url
					1, //size of view
					BitmapUtils.ROUND_NONE, //round all 
					0, // round size
					0, //shadow
					R.drawable.kard_shadow_default_horizontal, //shadow resource 
					0, 0); 

			viewHolder.btnAudience.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_AUDIENCE;
					msg.obj = item;
					mHandler.sendMessage(msg);
				}
			});
			
			viewHolder.drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
				
				@Override
				public void onDrawerOpened() {
					viewHolder.btnIcon.setImageResource(R.drawable.icon_row_right);
				}
			});
			viewHolder.drawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
				
				@Override
				public void onDrawerClosed() {
					viewHolder.btnIcon.setImageResource(R.drawable.icon_row_left);
				}
			});
			
			viewHolder.btnGive.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_REVIEW;
					msg.obj = item;
					mHandler.sendMessage(msg);
				}
			});
			viewHolder.btnStar.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_RATING;
					msg.obj = item;
					mHandler.sendMessage(msg);
				}
			});
			viewHolder.btnEdit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					viewHolder.drawer.close();
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_EDIT;
					msg.obj = item;
					mHandler.sendMessage(msg);
				}
			});
			viewHolder.btnDelete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					viewHolder.drawer.close();
					Message msg = new Message();
					msg.what = UIMessage.MSG_OPEN_DELETE;
					msg.obj = item;
					mHandler.sendMessage(msg);
				}
			});
		}
		return convertView;
	}

	private class ViewHolder {
		TextView mTextDetails;
		ImageView imgCoupon, btnAudience, btnGive, btnStar;
		ImageView btnIcon;
		Button btnEdit, btnDelete;
		RelativeLayout lyItemContent, lyItemCoupon;
		SlidingDrawer drawer;
	}

	Filter filter = new Filter() {

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			data = (ArrayList<ItemCouponsBuilder>) results.values;
			notifyDataSetChanged();
		}

		@SuppressLint("DefaultLocale")
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults result = new FilterResults();

			ArrayList<ItemCouponsBuilder> filterList = new ArrayList<ItemCouponsBuilder>();
			if (constraint.length() > 0) {
				for (ItemCouponsBuilder item : dataAll) {
					if (item.getCouponName().toLowerCase()
							.contains(constraint.toString().toLowerCase())) {
						filterList.add(item);
					}
				}
				result.values = filterList;
				return result;
			}

			result.values = dataAll;
			return result;
		}
	};

	@Override
	public Filter getFilter() {
		return filter;
	}

	@Override
	public int getPositionForSection(int section) {
		// TODO Auto-generated method stub
		if (section == 35) {
			return 0;
		}
		for (int i = 0; i < data.size(); i++) {
			String l = data.get(i).getCouponName();
			char firstChar = l.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

}
